libname cvs 'C:\Users\maggi\Documents\Manuscript\Work from home';
options NOFMTERR;

data wellbeing_p;
set cvs.wfh;

*Exposure: work from home (days/week);
work_home2018b=work_home2018;
if work_home2018 GE 1 then work_home2018b=1; *0, 1+ days;

work_home2018c=work_home2018;
if work_home2018>3 then work_home2018c=3; *0, 1, 2, 3+ days;

if work_home2018=0 then work_home2018d=0;
else if work_home2018 in (1, 2, 3, 4) then work_home2018d=1; 
else if work_home2018=5 then work_home2018d=2;  *0, 1-4 days, 5 days;

if work_home2018 in (0, 1, 2) then work_home2018e=work_home2018;
else if work_home2018 in (3, 4) then work_home2018e=3; 
else if work_home2018=5 then work_home2018e=4;   *0, 1, 2, 3-4, 5 days;

*Work distraction outcome (and prior value);
if distracted2019=1 then distracted2019_r=0;
else if distracted2019=2 then distracted2019_r=0.075;
else if distracted2019=3 then distracted2019_r=0.175;
else if distracted2019=4 then distracted2019_r=0.375;
else if distracted2019=5 then distracted2019_r=0.75;

if distracted2018=1 then distracted2018_r=0;
else if distracted2018=2 then distracted2018_r=0.075;
else if distracted2018=3 then distracted2018_r=0.175;
else if distracted2018=4 then distracted2018_r=0.375;
else if distracted2018=5 then distracted2018_r=0.75;

*Covariates;
if age_cat2018=1 then age_cat2018c=1; *<=30;
else if age_cat2018 in (2, 3) then age_cat2018c=2; *31-50;
else if age_cat2018=4 then age_cat2018c=3; *>50;

if race22018=0 then race22018c=1; *Non-Hispanic White;
else if race22018 in (1, 2) then race22018c=0; *others;

if mart_status2018 in (2, 6) then mart_status2018c=1; *married, partnership;
else if mart_status2018 in (1, 3, 4, 5) then mart_status2018c=0; *unmarried;

if educ2018=2 then educ2018c=1; *HS;
else if educ2018=3 then educ2018c=2; *some colleage;
else if educ2018 in (4, 5) then educ2018c=3; *college;
else if educ2018=6 then educ2018c=4; *graduate school;

if home2018=1 then home2018c=1; *yes;
else if home2018=2 then home2018c=0; *no;

if n_children2018>0 then n_children2018c=1;
else if n_children2018=0 then n_children2018c=0;

if n_elders2018>0 then elders2018c=1;
else if n_elders2018=0 then elders2018c=0;

if no_animal2018=1 then no_animal2018c=0;
else if no_animal2018=0 then no_animal2018c=1; *pet owner;

if work_hours2018=1 then work_hours2018c=1;*<8h;
else if work_hours2018=6 then work_hours2018c=2;*8h;
else if work_hours2018=2 then work_hours2018c=3;*9-10h;
else if work_hours2018 in (3, 4, 5) then work_hours2018c=4;*>10h;
run;

data wellbeing; *complete-case analysis;
set wellbeing_p;

if work_home2018=. then delete;
if nmiss (of distracted2019_r w_productive2019 wkfamilyconfl2019 jobsatis2019)>0 then delete;
if nmiss (of age_cat2018c gender2018 race22018c mart_status2018c educ2018c home2018c depressed2018 physhlth2018 
   n_children2018 n_elders2018 no_animal2018c work_hours2018c w_meaning2018 w_recognized2018 w_team2018 purpose2018 
   distracted2018_r w_productive2018 wkfamilyconfl2018 jobsatis2018)>0 then delete;

array cont[16] distracted2019_r w_productive2019 wkfamilyconfl2019 jobsatis2019 
               distracted2018_r w_productive2018 wkfamilyconfl2018 jobsatis2018 
               n_children2018 n_elders2018 purpose2018 depressed2018 physhlth2018 w_meaning2018 w_recognized2018 w_team2018;
array contz[16]  distracted2019_rz w_productive2019z wkfamilyconfl2019z jobsatis2019z 
                 distracted2018_rz w_productive2018z wkfamilyconfl2018z jobsatis2018z 
                 n_children2018z n_elders2018z purpose2018z depressed2018z physhlth2018z w_meaning2018z w_recognized2018z w_team2018z; 

do i=1 to 16; contz[i]=cont[i]; end;

proc standard mean=0 std=1 data=wellbeing out=wellbeing;
var distracted2019_rz w_productive2019z wkfamilyconfl2019z jobsatis2019z 
    distracted2018_rz w_productive2018z wkfamilyconfl2018z jobsatis2018z 
    n_children2018z n_elders2018z purpose2018z depressed2018z physhlth2018z w_meaning2018z w_recognized2018z w_team2018z;
run;

proc format;
value work_home2018b
      0= '0 days/week'
	  1= '>=1 day/week';

value work_home2018c
      0= '0 days/week'
	  1= '1 days/week'
	  2= '2 days/week'
      3= '3+ days/week';

value work_home2018d
      0= '0 days/week'
	  1= '1-4 days/week'
	  2= '5 days/week';

value work_home2018e
      0= '0 day/week'
	  1= '1 day/week'
	  2= '2 days/week'
      3= '3-4 days/week'
      4= '5 days/week';

value age_cat2018c
      1= '30 or below'
	  2= '31-50'
      3= 'above 50';

value gender2018c
      1= 'female'
	  2= 'male';

value race22018c 
      0= 'not non-Hispanic white'
	  1= 'white (not Hispanic)';

value mart_status2018c
      0= 'Unmarried'
      1= 'Married or in partnership';

value educ2018c
      1= 'high school diploma or eqivalent'
	  2= 'Some college but no degree'
	  3= 'College degree'
      4= 'Graduate school';

value home2018c
      0= 'no'
	  1= 'yes';

value n_children2018c
      0= 'no children'
      1= 'have children';	  

value elders2018c
      0= 'no elderly'
      1= 'have elderly';	

value no_animal2018c
	  0='no pet'
	  1='have pet';

value work_hours2018c 
      1= '<8h'
      2= '8h'
	  3= '9-10h'
      4= '>10h';
run;

proc freq data=wellbeing;
tables work_home2018 work_home2018c work_home2018b work_home2018d work_home2018e 
       age_cat2018c gender2018 race22018c mart_status2018c educ2018c home2018c n_children2018c elders2018c no_animal2018c work_hours2018c ;
format work_home2018c work_home2018c. work_home2018b work_home2018b. work_home2018d work_home2018d. work_home2018e work_home2018e.
       age_cat2018c age_cat2018c. gender2018 gender2018c. race22018c race22018c. mart_status2018c mart_status2018c. 
       educ2018c educ2018c. home2018c home2018c. n_children2018c n_children2018c. elders2018c elders2018c. no_animal2018c no_animal2018c. work_hours2018c work_hours2018c.;
title "Check binary/categorical covariates";
run;

proc means data=wellbeing;
var n_children2018 n_elders2018 purpose2018 depressed2018 physhlth2018 w_meaning2018 w_recognized2018 w_team2018
    distracted2018_r w_productive2018 wkfamilyconfl2018 jobsatis2018 distracted2019_r w_productive2019 wkfamilyconfl2019 jobsatis2019 ;
title "Check continuous variables (before standardizing)";
run;

proc means data=wellbeing;
var distracted2019_rz w_productive2019z wkfamilyconfl2019z jobsatis2019z 
    distracted2018_rz w_productive2018z wkfamilyconfl2018z jobsatis2018z 
    n_children2018z n_elders2018z purpose2018z depressed2018z physhlth2018z w_meaning2018z w_recognized2018z w_team2018z;
title "Check continuous variables (after standardizing)";
run;



